POW.nicename = "Flying"
POW.type = "togglable"
POW.help = "Press your normal move keys to fly. Jump is up."

function POW:KeyPress( key )
end

function POW:KeyRelease( key )
end

function POW:Initialize()
	Msg("Flying has been loaded!\n")
end

function POW:Disabled()
	self.owner:PrintMessage(3, self.owner:Name().. " no longer has " ..self.nicename.. ".")
	for k, v in pairs(player.GetAll()) do
		if ( v.currentpower.power and v.currentpower.power.nicename == "Flying" ) then
			return
		end
	end
	hook.Remove("PlayerShouldTakeDamage",  "Flying.NoFallDmg")
end

function POW:Enabled()
	hook.Add("PlayerShouldTakeDamage",  "Flying.NoFallDmg", self.NoFallDmg)
	self.owner:PrintMessage(3, self.owner:Name().. " now has " ..self.nicename.. ".")
end

function POW:Think()
	local vel = self.owner:GetVelocity():GetNormal()
	if( self.owner:KeyDown( IN_FORWARD ) ) then
		local dir = self.owner:GetAimVector() + Vector(0,0,0.4)
		vel = vel + dir * 40
	end
	if( self.owner:KeyDown( IN_BACK ) ) then
		local dir = self.owner:GetAimVector()
		vel = vel + dir * (-32) 
	end
	if( self.owner:KeyDown( IN_MOVELEFT ) ) then
		local dir = self.owner:GetRight()
		vel = vel + dir * (-26) 
	end
	if( self.owner:KeyDown( IN_MOVERIGHT ) ) then
		local dir = self.owner:GetRight()
		vel = vel + dir * 26
	end
	if( self.owner:KeyDown( IN_JUMP ) ) then
		local dir =  Vector(0,0,1)
		vel = vel + dir * 50 
	end
	
	--self.owner
	
	vel.x = math.Clamp( vel.x, -2000, 2000 )
	vel.y = math.Clamp( vel.y, -2000, 2000 )
	vel.z = math.Clamp( vel.z, -2000, 2000 )
	
	self.owner:SetVelocity( vel )
	
end

function POW.NoFallDmg( Victim, Attacker )
	if( Victim.currentpower.power and Victim.currentpower.power.nicename == "Flying" ) then
		if ( Attacker:IsWorld() ) then
			return false
		end 
	end
end
